import { defineReactive } from "../utils/Observe";
import { Vector3 } from "../math/Vector3";
import { Camera } from "./Camera";
import { MapKey, Mapper } from "../utils/Map";
import { post } from "../utils/Client";
/**轨道控制器 */
class OrbitControls {
  [x: string]: any;
  /**相机 */
  object: Camera = undefined;
  /**目标点 */
  target: Vector3 = undefined;
  /**中心点 */
  center: Vector3 = undefined;
  /**光标点 */
  cursor: Vector3 = undefined;
  @Mapper("mindistance,maxdistance")
  distance: {
    /**最短距离 */
    mindistance: number;
    /**最长距离 */
    maxdistance: number;
  };
  /** 最大缩放*/
  maxZoom: number = undefined;
  /** 最小缩放*/
  minZoom: number = undefined;
  /**缩放速度 */
  @Mapper("wheelspeed")
  zoomSpeed: number = undefined;
  /**旋转速度 */
  @Mapper("sliderspeed")
  rotateSpeed: number = undefined;

  constructor(options) {
    OrbitControls.prototype.isOrbitControls = true;
    Mapper.map(this, options);
    this.target = new Vector3();
    this.maxZoom = 10;
    this.minZoom = 1;
    this.object = options.camera;
    this.target = new Vector3();
    this.center = new Vector3();
    this.cursor = new Vector3();

    defineReactive(this, "zoomSpeed", (value, key) => {
      const { property } = Reflect.getMetadata(MapKey, this, key);
      this.putProperty(key, property,false, property);
    });
    defineReactive(this, "distance", (value, key) => {
      this.putProperty(key, key,true);
    });

    defineReactive(this, "rotateSpeed", (value, key) => {
      const { property } = Reflect.getMetadata(MapKey, this, key);
      this.putProperty(key, property,false, property);
    });
  }

  private putProperty(prop, key, expand?, alias?) {
    if (expand) {
      post("preview_controller", {
        ...this[prop],
        key,
      });
    } else {
      post("preview_controller", {
        [alias || prop]: this[prop],
        key,
      });
    }
  }
}

export { OrbitControls };
